package br.com.prime.model.bussines;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import br.com.prime.model.ConnectionFactory;
import br.com.prime.model.pojo.Aluno;

public class AlunoDAO {

	private Connection connection;

	public AlunoDAO() {
		try {
			this.connection = new ConnectionFactory().getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public List<Aluno> getAll() {
		List<Aluno> alunos = new ArrayList<Aluno>();
		String sql = "SELECT * FROM ALUNO";
		try {
			PreparedStatement stmt = this.connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			alunos = montarObjeto(rs);
			rs.close();
			stmt.close();
			return alunos;
		} catch (SQLException e) {
			e.printStackTrace();
			return alunos;
		}
	}
	
	public List<Aluno> montarObjeto(ResultSet rs){
		List<Aluno> alunos = new ArrayList<Aluno>();
		try {			
			while (rs.next()) {
				Aluno aluno = new Aluno();
				aluno.setID_ALUNO(rs.getInt("iD_ALUNO"));
				aluno.setNOME(rs.getString("NOME"));
				aluno.setRA(rs.getString("RA"));
				aluno.setID_ENDERECO(rs.getInt("ID_ENDERECO"));
				
				alunos.add(aluno);
			}	
			return alunos;
		} catch (Exception e) {
			e.printStackTrace();
			return alunos;
		}		
	}
}